package ysy.aspectj.aspectj;

import android.util.Log;
import android.widget.TextView;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.annotation.Annotation;

import ysy.aspectj.annotation.AspectJAnnotation;

@Aspect
public class MyAspectJ {

    private static final String TAG = "tag";

    @Pointcut("call(@ysy.aspectj.annotation.AspectJAnnotation * *(..))")
    public void annoBaviorTrace(){

    }

    @Around("annoBaviorTrace()")
    public Object onGetRunTime(ProceedingJoinPoint joinPoint){

        try {
            MethodSignature signature = (MethodSignature)joinPoint.getSignature();
//            AspectJAnnotation annotation = (AspectJAnnotation) signature.getDeclaringType().getAnnotation(AspectJAnnotation.class);
//            String value="";
//            if (annotation!=null){
//                value=annotation.value();
//            }
            String className = signature.getDeclaringType().getSimpleName();
            String methodName = signature.getName();
            long start = System.currentTimeMillis();
            Object proceed = joinPoint.proceed();
            long l = System.currentTimeMillis() - start;
            Log.e(TAG, "onGetRunTime: 类："+className+" 方法："+methodName+" 时间:"+l );
            if (proceed instanceof TextView){
                ((TextView) proceed).setText("aspectj 修改数据");
            }
            Log.e(TAG, "proceed: "+proceed);
            return proceed;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }

}
